package cn.jc.spider.controller;


import java.io.IOException;
import java.sql.Time;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.torque.TorqueException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.jc.spider.neww.xiaohua.WebService;
import cn.jc.spider.po.DataXiaohua;
import cn.jc.spider.util.Page;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.TimeTypeAdapter;


/**
 * Handles requests for the application home page.
 */
@Controller
public class EntityController_xiaohua {

	private static final Logger logger = Logger.getLogger(EntityController_xiaohua.class); 
	
	@RequestMapping(value="/get_xiaohua0.do")
	public String getChannelList_json(HttpServletRequest request,HttpServletResponse response) throws TorqueException {
		Page page = getPageInfo(request);
		List<DataXiaohua> xhs = WebService.queryXiaoHua(page);
		writejson(toJson(warpJson(xhs), ""),response);
		return null;
	}
	@RequestMapping(value="/get_xiaohua.do")
	public String getChannelList_jsonp(HttpServletRequest request,HttpServletResponse response) throws TorqueException {
		String cb = request.getParameter("jsoncallback");
		Page page = getPageInfo(request);
		List<DataXiaohua> xhs = WebService.queryXiaoHua(page);
		String jsonString = toJson(warpJson(xhs), "");
		String jsonpString = cb+"("+jsonString+")";
		writejson(jsonpString,response);
		return null;
	}
	protected Page getPageInfo(HttpServletRequest request) {
		//每页个数
		int size = StringUtils.isBlank(request.getParameter("size"))?20:
			Integer.parseInt(request.getParameter("size"));
		int curPage = StringUtils.isBlank(request.getParameter("p"))?1:Integer.parseInt(request.getParameter("p"));
		Page page = new Page();
		page.setSize(size);
		page.setCurrentpage(curPage);
		
		request.setAttribute("page", page);
		String baseurl = getbaseurl(request,true);
		page.setBaseurl(baseurl);
		return page;
	}
	private String getbaseurl(HttpServletRequest request, boolean forpage) {
		String baseurl = request.getRequestURI();
		Map sss = request.getParameterMap();
		if (MapUtils.isNotEmpty(sss)) {
			baseurl += "?"; 
		}
		for (Object s : sss.keySet()) {
			if (forpage){
				if ("p".equals(s)) {
					continue;
				}
			}
			baseurl += s + "=" + request.getParameter((String) s) + "&";
		}
		if (baseurl.endsWith("&")) {
			baseurl = baseurl.substring(0, baseurl.length() - 1);
		}
		if (baseurl.endsWith("?")) {
			baseurl = baseurl.substring(0, baseurl.length() - 1);
		}
		return baseurl;
	}
	private Object warpJson(List<DataXiaohua> ts) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", CollectionUtils.isNotEmpty(ts) ? ts.size() : 0);
		map.put("rows", ts);
		return map;
	}
	/**
	 * MoveToUtil
	 * @param result
	 * @param response
	 * @author JiangChi
	 */
	protected void writejson(String result, HttpServletResponse response) {
		try {
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * MoveToUtil
	 * @param s
	 * @param desc
	 * @return
	 * @author JiangChi
	 */
	public static String toJson(Object s, final String desc) {
		GsonBuilder v = new GsonBuilder();
		v.setDateFormat("yyyy-MM-dd HH:mm:ss");
		v.registerTypeAdapter(Time.class, TimeTypeAdapter.FACTORY);
		v.setExclusionStrategies(new ExclusionStrategy(){
			@Override
			public boolean shouldSkipClass(Class<?> arg0) {
				return false;
			}
			@Override
			public boolean shouldSkipField(FieldAttributes attr) {
				if (desc.contains("|" + attr.getName() + "|")) {
					return true;
				}
				return false;
			}
		});
		String ss = v.create().toJson(s);
		return ss;
	}
}

